package com.jinzecloud.reportapproval.service.impl;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.jinzecloud.reportapproval.bean.ProductVerificaRecord;
import com.jinzecloud.reportapproval.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.jinzecloud.reportapproval.mapper.DeepStabilityRecordMapper;
import com.jinzecloud.reportapproval.bean.DeepStabilityRecord;
import com.jinzecloud.reportapproval.service.DeepStabilityRecordService;

import javax.servlet.http.HttpServletRequest;


@Service("deepStabilityRecordService")
public class DeepStabilityRecordServiceImpl extends ServiceImpl<DeepStabilityRecordMapper, DeepStabilityRecord> implements DeepStabilityRecordService {
    @Autowired
    private FileUploadUtil fileUploadUtil;
    @Autowired
    private HttpServletRequest request;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DeepStabilityRecord> page = this.page(
                new Query<DeepStabilityRecord>().getPage(params),
                new QueryWrapper<DeepStabilityRecord>().like("project_num",params.get("key"))
        );

        return new PageUtils(page);
    }

    @Override
    public String createPdf(Integer id) {
        DeepStabilityRecord deepStabilityRecord=getById(id);
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(deepStabilityRecord));
        // 转换 gaugingPoint 数据为可循环的格式
        JSONObject gaugingPointData = data.getJSONObject("gaugingPoint");
        List<Map<String, Object>> gaugingPointList = new ArrayList<>();

        // 将 gaugingPoint 转换为 List<Map> 格式
        gaugingPointData.forEach((key, value) -> {
            Map<String, Object> map = new HashMap<>();
            map.put("point", Integer.parseInt(key)+1);
            map.put("left", ((JSONObject) value).getInteger("left"));
            map.put("right", ((JSONObject) value).getInteger("right"));
            gaugingPointList.add(map);
        });

        // 更新 data，绑定 gaugingPoint 列表
        data.put("gaugingPoint", gaugingPointList);
        //word
        File tempFile = WordUtil.getWordFile("template/deepstability.docx", data, Configure.builder().bind("gaugingPoint", policy));

        if (tempFile == null) return null;
        String fileName = "耕深稳定性测定记录表";
        String uploadDirectory = fileUploadUtil.getUploadFilePath("temp");
        String filePath = uploadDirectory + deepStabilityRecord.getProjectNum() + fileName;
        String savePath = RequestUtils.getBasePath(request) + "upload/temp/" + deepStabilityRecord.getProjectNum() +fileName;
        FileUtil.copy(tempFile, new File(filePath + ".docx"),true);
        PDFUtil.WordToPdf(tempFile, new File(filePath + ".pdf"));

        update(new LambdaUpdateWrapper<DeepStabilityRecord>().set(DeepStabilityRecord::getWordPath, savePath + ".docx").set(DeepStabilityRecord::getPdfPath, savePath + ".pdf").eq(DeepStabilityRecord::getId, id));
         return RequestUtils.getBasePath(request) + "upload/temp/" + deepStabilityRecord.getProjectNum() + fileName + ".pdf" ;
    }

    @Override
    public JSONObject deepStabilityRecordJson(String projectUuid) {
        DeepStabilityRecord deepStabilityRecord=getOne(new QueryWrapper<DeepStabilityRecord>().eq("project_num", projectUuid));
        if (deepStabilityRecord == null) return null;
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(deepStabilityRecord));
        // 转换 gaugingPoint 数据为可循环的格式
        JSONObject gaugingPointData = data.getJSONObject("gaugingPoint");
        List<Map<String, Object>> gaugingPointList = new ArrayList<>();

        // 将 gaugingPoint 转换为 List<Map> 格式
        gaugingPointData.forEach((key, value) -> {
            Map<String, Object> map = new HashMap<>();
            map.put("point", Integer.parseInt(key)+1);
            map.put("left", ((JSONObject) value).getInteger("left"));
            map.put("right", ((JSONObject) value).getInteger("right"));
            gaugingPointList.add(map);
        });

        // 更新 data，绑定 gaugingPoint 列表
        data.put("gaugingPoint", gaugingPointList);

        return data;
    }

    public DeepStabilityRecord getProductNumber(String projectNumber) {
        DeepStabilityRecord deepStabilityRecord=getOne(new QueryWrapper<DeepStabilityRecord>().eq("project_num", projectNumber));
        return deepStabilityRecord;
    }
}